package compilers.AST.expression;

import java.util.Iterator;
import java.util.LinkedList;

import compilers.AST.Node;
import compilers.AST.types.MyType;
import compilers.AST.types.TypeSpecifier;
import compilers.compiler.PGenerator;
import compilers.exceptions.SemanticalException;
import compilers.symbolTable.SymbolTableScope;

public class Mult extends Node {
	
	public final static int MULT = 1;
	public final static int DIV = 2;

	private Unary fFirstUnary;
	private LinkedList<Integer> fOperators;
	private LinkedList<Unary> fSecondUnaries;
	
	public Mult() {
		fOperators = new LinkedList<Integer>(); 
		fSecondUnaries = new LinkedList<Unary>();
	}
	
	public Mult(int lineNr) {
		super(lineNr);
		fOperators = new LinkedList<Integer>(); 
		fSecondUnaries = new LinkedList<Unary>();
	}
	
	public void print(int identLevel) {
		println(identLevel, "Mult");
		fFirstUnary.print(identLevel+1);
		if (!fOperators.isEmpty()) {
			Iterator<Integer> itOp = fOperators.iterator();
			Iterator<Unary> itUn = fSecondUnaries.iterator();
			while (itOp.hasNext()) {
				String opName = "";
				switch (itOp.next()) {
					case MULT:
						opName = "*";
						break;
					case DIV:
						opName = "/";
						break;
				}
				println(identLevel+1, "Operation = " + opName);
				itUn.next().print(identLevel+1);
				}
		}
	}
	
	public void checkSemantics(SymbolTableScope currentScope) throws SemanticalException {
		fFirstUnary.checkSemantics(currentScope);
		// Check types
		if (!fOperators.isEmpty()) {
			if ((fFirstUnary.getResultType(currentScope) != TypeSpecifier.INTEGER) && (fFirstUnary.getResultType(currentScope) != TypeSpecifier.FLOAT)) {
				throw new SemanticalException("The type in the multiplication expression is not of the requested numerical types.", fLineNr);
			}
			Iterator<Unary> itUn = fSecondUnaries.iterator();
			while (itUn.hasNext()) {
				Unary un = itUn.next();
				un.checkSemantics(currentScope);
				if ((un.getResultType(currentScope) != TypeSpecifier.INTEGER) && (un.getResultType(currentScope) != TypeSpecifier.FLOAT)) {
					throw new SemanticalException("The type in the multiplication expression is not of the requested numerical types.", fLineNr);
				}
			}
		}
	}
	
	public void addOperator(int operation) {
		fOperators.add(operation);
	}
	
	public void setFirstUnary(Unary unary) {
		fFirstUnary = unary;
	}
	
	public void addSecondUnary(Unary unary) {
		fSecondUnaries.add(unary);
	}
	
	public int getResultType(SymbolTableScope currentScope) {
		if (!fOperators.isEmpty()) {
			if (fFirstUnary.getResultType(currentScope) == TypeSpecifier.FLOAT) {
				return TypeSpecifier.FLOAT;
			}
			Iterator<Unary> itUn = fSecondUnaries.iterator();
			while (itUn.hasNext()) {
				Unary un = itUn.next();
				if (un.getResultType(currentScope) == TypeSpecifier.FLOAT) {
					return TypeSpecifier.FLOAT;
				}
			}
			return TypeSpecifier.INTEGER;
		} else {
			return fFirstUnary.getResultType(currentScope);
		}
	}
	
	public int getOriginalType(SymbolTableScope currentScope) {
		if (!fOperators.isEmpty()) {
			if (fFirstUnary.getOriginalType(currentScope) == TypeSpecifier.FLOAT) {
				return TypeSpecifier.FLOAT;
			}
			Iterator<Unary> itUn = fSecondUnaries.iterator();
			while (itUn.hasNext()) {
				Unary un = itUn.next();
				if (un.getOriginalType(currentScope) == TypeSpecifier.FLOAT) {
					return TypeSpecifier.FLOAT;
				}
			}
			return TypeSpecifier.INTEGER;
		} else {
			return fFirstUnary.getOriginalType(currentScope);
		}
	}
	
	public boolean isConstant(SymbolTableScope currentScope) {
		if(fOperators.isEmpty()) {
			return fFirstUnary.isConstant(currentScope);
		} else {
			if (!fFirstUnary.isConstant(currentScope)) {
				return false;
			}
			Iterator<Unary> itUn = fSecondUnaries.iterator();
			while (itUn.hasNext()) {
				if (!itUn.next().isConstant(currentScope)) {
					return false;
				}
			}
			return true;
		}
	}
	

	public boolean isArray(SymbolTableScope currentScope) {
		if (fOperators.isEmpty()) {
			return fFirstUnary.isArray(currentScope);
		} else {
			return false;
		}
	}
	
	public String getArrayIdent(SymbolTableScope currentScope) {
		return fFirstUnary.getArrayIdent(currentScope);
	}
	
	public MyType calculateValue(SymbolTableScope currentScope) {
		if(fOperators.isEmpty()) {
			return fFirstUnary.calculateValue(currentScope);
		} else {
			Iterator<Integer> itOp = fOperators.iterator();
			Iterator<Unary> itUn = fSecondUnaries.iterator();
			MyType result = fFirstUnary.calculateValue(currentScope);
			while (itUn.hasNext()) {
				Unary un = itUn.next();
				if(itOp.next() == MULT) {
					result = result.mult(un.calculateValue(currentScope));
				} else {
					result = result.div(un.calculateValue(currentScope));
				}
			}
			return result;
		}
	}
	
	public void generatePcode(PGenerator pGen, SymbolTableScope currentScope) {
		// Get resulttype and cast every term to this type if necessary
		int resultType = this.getResultType(currentScope);
		
		// First mult
		if (fFirstUnary.isConstant(currentScope)) {
			TypeSpecifier firstResultType = new TypeSpecifier(fFirstUnary.getResultType(currentScope));
			pGen.load(firstResultType.getPType(currentScope), fFirstUnary.calculateValue(currentScope).valueToString());
		} else {
			fFirstUnary.generatePcode(pGen, currentScope);
		}
		if (fFirstUnary.getResultType(currentScope) != resultType) {
			pGen.conv(TypeSpecifier.getPType(fFirstUnary.getOriginalType(currentScope)), TypeSpecifier.getPType(resultType));
		}
		
		// Second mults
		if (!fOperators.isEmpty()) {
			Iterator<Integer> itOp = fOperators.iterator();
			Iterator<Unary> itUn = fSecondUnaries.iterator();
			while (itUn.hasNext()) {
				Unary un = itUn.next();
				if (un.isConstant(currentScope)) {
					pGen.load(TypeSpecifier.getPType(un.getResultType(currentScope)), un.calculateValue(currentScope).valueToString());
				} else {
					un.generatePcode(pGen, currentScope);
				}
				if (un.getResultType(currentScope) != resultType) {
					pGen.conv(TypeSpecifier.getPType(un.getOriginalType(currentScope)), TypeSpecifier.getPType(resultType));
				}
				if (itOp.next() == MULT) {
					pGen.mult(TypeSpecifier.getPType(resultType));
				} else {
					pGen.div(TypeSpecifier.getPType(resultType));
				}
			}
		}
	}

}
